var path = require("path");
var webpack = require("webpack");
var glob = require("glob");

//打包css
var ExtractTextPlugin = require("extract-text-webpack-plugin");
//打包html
var HtmlWebpackPlugin = require('html-webpack-plugin');
//输出路径 土豆：修改为views
//var disPath = path.join(__dirname, "dist");
var disPath = path.join(__dirname, "../views/dist");
var conf = require("../mod/config");
var hash = ""; //名字加hash 土豆修改
var pages = getEntry('./src/views/**/**.html', "views");
var entrys = getEntry('./src/js/entry/**/**.js', "entry"); //入口文件
entrys['swiper'] = path.join(__dirname,"./src/js/lib/swiper.min");
//单独打包三方插件
var other = {
  "local_index":["swiper"],
  "audio_visual":["swiper"]
}

// 丑化代码使用hash 土豆修改
if(conf.ugly){
  hash = ".[hash:5]";
}
var config = {
  entry: entrys,
  output: {
    path: disPath,
    publicPath: "<%=conf.publicPath%>",
    filename: "assets/js/[name]" + hash + ".js"
  },
  resolve: {
    extensions: ['', '.js', '.css', 'scss'],
    //别名
    alias: {
      
    }
  },
  module: {
    loaders: [
    {
      test: /\.html$/,
      loader: "html",
      exclude: /node_modules/,
      include: path.join(__dirname, "/src")
    },
    /*html*/
    {
      test: /\.js$/,
      loader: "babel",
      exclude: /node_modules/,
      include: path.join(__dirname, "/src")
    },
    /*es6 to es5*/
    {
      test: /\.scss$/,
      loader: ExtractTextPlugin.extract("style-loader", 'css!sass'),
      exclude: /node_modules/,
      include: path.join(__dirname, "/src")
    },
    {
      test: /\.css$/,
      loader: ExtractTextPlugin.extract("style-loader", "css-loader"),
      exclude: /node_modules/,
      include: path.join(__dirname, "/src")
    },
    {
      test: /\.(jpg|jpe?g|png|gif|svg)$/,
      loader: 'url',
      query: {
        limit: 10000,
        name: 'assets/img/[name]' + hash + '.[ext]',
        publicPath:"/"
      },
      exclude: /node_modules/,
      include: path.resolve(__dirname, 'src')
    }]
  },
  // 转化成es5的语法
  babel: {
    presets: ['es2015', 'stage-3'], //转码规则，ES7语法转码规则
    plugins: ['transform-runtime']
  },
  //webpack 插件
  plugins: [
    new webpack.ProvidePlugin({
      $: path.resolve(__dirname,'./src/js/lib/zepto/zepto.min'),
      jQuery: path.resolve(__dirname,'./src/js/lib/zepto/zepto.min'),
      'window.jQuery': path.resolve(__dirname,'./src/js/lib/zepto/zepto.min'),
      'window.$': path.resolve(__dirname,'./src/js/lib/zepto/zepto.min'),
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common'
    }),
    new ExtractTextPlugin('assets/css/[name]'+hash+'.css', {
      allChunks: false
    })
  ]
};

for (var chunk in pages) {
  var chunks = ['common', chunk];
  if (chunk.indexOf('public') != -1 || chunk.indexOf('404') != -1) {
    chunks = [''];
  }
  if(other[chunk]){
    chunks = chunks.concat(other[chunk]);
  }
  config.plugins.push(new HtmlWebpackPlugin({
    filename: '../../views/' + chunk + ".html",
    template: pages[chunk],
    inject: true, //可对head和body修改
    chunks: chunks,
    minify: { }, //压缩HTML
    hash: false
  }));
}

if (conf.ugly) {
  config.plugins.push(new webpack.optimize.UglifyJsPlugin({
    compress: {
      warnings: false
    },
    minimize: true
  }));
}

//拆分文件路径
function getEntry(url, cut, tail) {
  var entry = {};
  glob.sync(url).forEach(function(name) {
    var key = "",
        value = "";
    var str = name.split(cut)[1];
    key = str.substring(1, str.lastIndexOf('.'));
    key = key.split("-")[0].split(".")[0];
    // var str = name.split(cut)[1];
    // key = str.substring(1,str.lastIndexOf('.'));
    value = __dirname + name.substring(1);
    key = tail ? (key+"-"+tail) : key;
    if (key != "" && key.indexOf(".") == -1) {
        entry[key] = value;
    }
  });
  return entry;
}

var cssModele = getEntry("./src/css/**/*css","css","css");
var jsModele = getEntry("./src/js/modules/**/*js","modules");
var jsLibModele = getEntry("./src/js/lib/**/*js","lib");
Object.assign(config.resolve.alias,cssModele,jsModele,jsLibModele);
//console.log(cssModele)

module.exports = config;
